﻿using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace DNNtc.MSBuild.Tasks
{
    public class GetVersionInfo : Task
    {
        [Required]
        public string Version { get; set; }

        [Output]
        public string Major { get; private set; }

        [Output]
        public string Minor { get; private set; }

        [Output]
        public string Build { get; private set; }
        
        public override bool Execute()
        {
            if (Version.ToLower().Contains("assemblyversion"))
            {
                //[assembly: AssemblyVersion("01.00.02.0")]
                //Result = 1.0.2.0
                //OR
                //<Assembly: AssemblyVersion("5.0.4.*")> 
                //Result = 5.0.4.*
                
                //Remove the first character
                string av = Version.ToLower().Trim();
                av = av.Substring(1);

                //Remove redundant information
                av = av.Replace("assembly: assemblyversion(\"", "").Trim();
                
                //Remove last 3 characters
                av = av.Substring(0, av.Length - 3);

                //Get the numbers in a array of strings
                string[] nrs = av.Split('.');
                
                int major,minor,build;

                //Set the numbers
                int.TryParse(nrs[0], out major);
                int.TryParse(nrs[1], out minor);
                int.TryParse(nrs[2], out build);
                
                //Set return variables
                Major = major.ToString();
                Minor = minor.ToString();
                Build = build.ToString();
            }
            return true;
        }
    }

    public class SetVersionInfo : Task
    {
        [Required]
        public string Version { get; set; }

        [Output]
        public string Major { get; private set; }

        [Output]
        public string Minor { get; private set; }

        [Output]
        public string Build { get; private set; }

        public override bool Execute()
        {
            if (Version.Contains("AssemblyVersion"))
            {
                //[assembly: AssemblyVersion("01.00.02.0")]
                //Result = 1.0.2.0
                //OR
                //<Assembly: AssemblyVersion("5.0.4.*")> 
                //Result = 5.0.4.*

                //Remove the first character
                string av = Version.ToLower().Trim();
                av = av.Substring(1);

                //Remove redundant information
                av = av.Replace("assembly: assemblyversion(\"", "").Trim();

                //Remove last 3 characters
                av = av.Substring(0, av.Length - 3);

                //Get the numbers in a array of strings
                string[] nrs = av.Split('.');

                int major, minor, build;

                //Set the numbers
                int.TryParse(nrs[0], out major);
                int.TryParse(nrs[1], out minor);
                int.TryParse(nrs[2], out build);

                //Update Build
                build += 1;

                //Set return variables
                Major = major.ToString();
                Minor = minor.ToString();
                Build = build.ToString();
            }
            return true;
        }
    }
}
